#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
const char *wday[] = 
{ "星期天","星期一","星期二","星期三","星期四","星期五","星期六"
};

     

void showMsg(int* msg);
void showHead(void);//信息头
void showTail(void);

int main(int argc, char **argv)
{
     int fd_memfifo;
     
     fd_memfifo = open("/dev/memfifo",O_RDWR);
     if(fd_memfifo == -1)
     {
	  printf("open /dev/memfifo failed\n");
	  
	  return -1;
	  
     }
     
     int msg[4];
     int ret;
     int i;
     
     while(1)
     {
	  showHead();
	  
	  for(i = 0; i< 5;i++)
	  {
	       
	       read(fd_memfifo,(void*)msg,4*4);
	        showMsg(msg);
	  }
	  
	  
	  showTail();
	  
	  sleep(5); //等待5s
	     
     }
     
     
  return 0;
}


void showMsg(int* msg)
{
     
     int wait_hour;
     int wait_min;
     
      time_t now;
     struct tm *p;
     time(&now);
     p = localtime(&now);//取得当前时间

     wait_hour = p->tm_hour - msg[2];
     wait_min  = p->tm_min  - msg[3];
     
     
     printf("       %d              %d           %02d:%02d        %d小时%d分         \n",msg[0],msg[1],msg[2],msg[3],wait_hour,wait_min);
}

void showHead(void)
{
     system("clear");
     
      printf("************************************************************\n");
     printf("*                    公交信息发布系统                      *\n");
     printf("公交车次    现在所在站点      大概到站时间   大概等待时间               \n");
     
}

void showTail(void)
{
     time_t now;
     struct tm *p;
     time(&now);
     p = localtime(&now);//取得当前时间
     
     printf( "现在时间 %d-%d-%d %s %d:%d                               荆州公交总公司\n",(1900+p->tm_year),(1 + p->tm_mon),p->tm_mday,wday[p->tm_wday],p->tm_hour,p->tm_min  );
}
